package com.gxa.guahao01.service;

import com.gxa.guahao01.mapper.HuanZheJiuZhenKaMapper;
import com.gxa.guahao01.pojo.GuaHaoJiLu;
import com.gxa.guahao01.mapper.GuaHaoJiLuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 患者挂号记录(GuaHaoJiLu)表服务实现类
 *
 * @author xyc
 * @since 2021-03-17 10:53:41
 * @version 1.0
 */
@Service("guaHaoJiLuService")
public class GuaHaoJiLuServiceImpl  {
    @Resource
    private GuaHaoJiLuMapper guaHaoJiLuMapper;
    @Resource
    private HuanZheJiuZhenKaMapper huanZheJiuZhenKaMapper;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 根据模糊条件查询总个数
     *
     * @return 返回查询到的总个数
     */
    public Map<String, Object> chaXunCount(String mingCheng) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("list", this.guaHaoJiLuMapper.chaXunCount(mingCheng));
        return map;
    }

    /**
     * 查询所有数据
     * @return  返回所有数据
     */
    public Map<String, Object> chaXunAll(String kaiShiShiJian,String jieShuShiJian,String jieZhenJiLuZhuangTai) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("list", this.guaHaoJiLuMapper.chaXunAll(kaiShiShiJian,jieShuShiJian,jieZhenJiLuZhuangTai));
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public Map<String, Object> chaXunById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("obj", this.guaHaoJiLuMapper.chaXunById(id));
        return map;
    }

    /**
     * 查询分页数据
     *
     * @param page 查询起始位置
     * @param mingCheng 查询条数
     * @return 对象列表
     */
//    public Map<String, Object> chaXunFenYe(int page, String mingCheng) {
//    // 获取当前表中的总记录
//        int tableCount = this.guaHaoJiLuMapper.chaXunCount(mingCheng);
//        // 总页码计算   (总条数 - 1) / 每页显示条数  + 1
//        // (100 - 1) / 10 + 1 = 10        (101 - 1) / 10 + 1 = 11      (99 - 1) / 10 + 1 = 10
//        int pageCount = (tableCount - 1) / 10 + 1;
//        // 计算每页开始的下标值
//        int xiaBiao = (page - 1) * 10;
//        Map<String, Object> map = new HashMap<>();
//        map.put("code", 0);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
//        map.put("msg", "查询成功");
//        map.put("pageCount", pageCount);  // 查询的记录总页码
//        map.put("count", tableCount);     // 当前表中的总条数
//        map.put("data", this.guaHaoJiLuMapper.chaXunFenYe(xiaBiao, mingCheng));
//        return map;
//    }

    /**
     * 新增数据
     *
     * @param guaHaoJiLu 实例对象
     * @return 实例对象
     */
    @Transactional
    public Map<String, Object> xinZeng(GuaHaoJiLu guaHaoJiLu) {
        String uuid = "";
        String guaHaoYuanId = guaHaoJiLu.getGuaHaoYuanId().toString();
        uuid = uuid + new Date(System.currentTimeMillis()).getTime() + (int)(Math.random()*10000) + guaHaoYuanId;
        guaHaoJiLu.setGuaHaoDanHao(uuid);
        //获取前端传递的患者卡号
        String huanZheKaHao = guaHaoJiLu.getHuanZheJiuZhenKa().getHuanZheKaHao();
        //查询是否为一个新的卡号
        int count = this.guaHaoJiLuMapper.chaXunCount(huanZheKaHao);
        //如果是则新增一个患者
        if (count == 0){
            //先将传递来的患者信息新增到患者就诊卡表
            this.huanZheJiuZhenKaMapper.xinZeng(guaHaoJiLu.getHuanZheJiuZhenKa());
            //再将挂号记录中的患者信息设为null
            guaHaoJiLu.setHuanZheJiuZhenKa(null);
            //查询到刚刚新增的患者的id
            int id = this.huanZheJiuZhenKaMapper.chaXunIdByKaHao(huanZheKaHao);
            //将患者信息绑定到就诊记录
            guaHaoJiLu.setHuanZheJiuZhenKaId(id);
            //就诊状态设为未就诊
            guaHaoJiLu.setJiuZhenJiLuZhuangTai(1);
            //新增这条挂号记录
            this.guaHaoJiLuMapper.xinZeng(guaHaoJiLu);
//            redisTemplate.opsForZSet().incrementScore("guahaocount",new SimpleDateFormat("yyyy:MM:dd").format(new Date(System.currentTimeMillis())),1);
//            redisTemplate.opsForZSet().incrementScore("guahaocount",new SimpleDateFormat("yyyy:MM").format(new Date(System.currentTimeMillis())),1);
//            redisTemplate.opsForZSet().incrementScore("ghuahaopayment",new SimpleDateFormat("yyyy:MM:dd").format(new Date(System.currentTimeMillis())),guaHaoJiLu.getFeiYongHeJi());
//            redisTemplate.opsForZSet().incrementScore("ghuahaopayment",new SimpleDateFormat("yyyy:MM").format(new Date(System.currentTimeMillis())),guaHaoJiLu.getFeiYongHeJi());

        }
        //如果卡号已存在则不用新增患者
        else{
            //将挂号记录中的患者信息设为null
            guaHaoJiLu.setHuanZheJiuZhenKa(null);
            //查询到此患者的id
            int id = this.huanZheJiuZhenKaMapper.chaXunIdByKaHao(huanZheKaHao);
            //将患者信息绑定到就诊记录
            guaHaoJiLu.setHuanZheJiuZhenKaId(id);
            //就诊状态设为未就诊
            guaHaoJiLu.setJiuZhenJiLuZhuangTai(1);
            //新增这条挂号记录
            this.guaHaoJiLuMapper.xinZeng(guaHaoJiLu);
//            redisTemplate.opsForZSet().incrementScore("guahaocount",new SimpleDateFormat("yyyy:MM:dd").format(new Date(System.currentTimeMillis())),1);
//            redisTemplate.opsForZSet().incrementScore("guahaocount",new SimpleDateFormat("yyyy:MM").format(new Date(System.currentTimeMillis())),1);
//            redisTemplate.opsForZSet().incrementScore("ghuahaopayment",new SimpleDateFormat("yyyy:MM:dd").format(new Date(System.currentTimeMillis())),guaHaoJiLu.getFeiYongHeJi());
//            redisTemplate.opsForZSet().incrementScore("ghuahaopayment",new SimpleDateFormat("yyyy:MM").format(new Date(System.currentTimeMillis())),guaHaoJiLu.getFeiYongHeJi());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "新增成功");
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param guaHaoJiLu 实例对象
     * @return 实例对象
     */
    public Map<String, Object> gengXinById(GuaHaoJiLu guaHaoJiLu) {
        this.guaHaoJiLuMapper.gengXinById(guaHaoJiLu);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public Map<String, Object> shanChuById(String id) {
        this.guaHaoJiLuMapper.shanChuById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "退号成功");
        return map;
    }
    public void check(){
        redisTemplate.opsForZSet().incrementScore("guahaocount","2021:02:17",1);
        redisTemplate.opsForZSet().incrementScore("guahaocount","2021:02",1);
        redisTemplate.opsForZSet().incrementScore("ghuahaopayment","2021:02:17",50);
        redisTemplate.opsForZSet().incrementScore("ghuahaopayment","2021:02",50);
        System.out.println(redisTemplate.opsForZSet().score("guahaocount","2021:02:17"));
        System.out.println(redisTemplate.opsForZSet().score("guahaocount","2021:02"));
        System.out.println(redisTemplate.opsForZSet().score("ghuahaopayment","2021:02:17"));
        System.out.println(redisTemplate.opsForZSet().score("ghuahaopayment","2021:02"));
    }
}